import 'package:animation/api/update.dart';
import 'package:animation/components/button/normal_button.dart';
import 'package:animation/generated/l10n.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';


class UpdateApk {
  String updateUrl =
      'https://gitee.com/zsnoin-can/new-wall-paper-apk/blob/master/wallpaper.apk';

  Future updateApk(BuildContext context, {bool displayTips = false}) async {
    Response res = await updateApp();
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String version = packageInfo.version; // 获取版本号
    if (version != res.data['version']) {
      showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                spacing: 5,
                children: [
                  Icon(Icons.update, color: Colors.deepOrangeAccent),
                  Text(
                    S.of(context).s15,
                    style: TextStyle(
                      fontSize: 18,
                    ),
                  ),
                  Text(
                    '${res.data['version']}',
                    style: TextStyle(
                      color: Theme.of(context).colorScheme.primary,
                      fontSize: 16,
                    ),
                  ),
                ],
              ),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(5)),
              ),
              content: SizedBox(
                height: 100,
                child: Padding(
                  padding: const EdgeInsets.only(left: 10, right: 10),
                  child: ListView.builder(
                      itemCount: res.data['update'].length,
                      itemBuilder: (context, index) {
                        return Text(
                            "${index + 1}、${res.data['update'][index]}");
                      }),
                ),
              ),
              actions: [
                NormalButton(
                    radius: 5,
                    width: 100,
                    height: 30,
                    fontSize: 16,
                    title: S.of(context).b7,
                    bgColor: const Color.fromARGB(131, 158, 158, 158),
                    onPressed: () {
                      Navigator.pop(context);
                    }),
                NormalButton(
                    radius: 5,
                    width: 100,
                    height: 30,
                    fontSize: 16,
                    title: S.of(context).s16,
                    onPressed: () async {
                      // 打开浏览器更新
                      if (await canLaunchUrl(Uri.parse(updateUrl))) {
                        await launchUrl(Uri.parse(updateUrl));
                      } else {
                        // 如果无法打开链接，显示错误提示
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text('url error: $updateUrl')),
                        );
                      }
                      Navigator.pop(context);
                    }),
              ],
            );
          });
    }
    if (version == res.data['version'] && displayTips) {
      BotToast.showText(text: S.of(context).s17);
    }
  }
}
